package com.test.jbpm.web.action;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.util.Streams;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.test.jbpm.dao.impl.UserDaoImpl;
import com.test.jbpm.domain.User;


@Controller
@Scope("prototype")
public class fileUpAction extends BaseAction {
     
	private static final long serialVersionUID = 1L;
	private File[] file;
	private String[] uploadFileNames;
	private String[] uploadContentTypes;
	private String fileFileName; 
	private String fileContentType; 
	private User user;
	private HttpServletResponse response ;  

	
	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}


	public String index() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 文件上传并存储到数据库
	 */
	public void fileUp1(){
		
		try {
			fileUp11();    //先上传头像到数据库
			UserDaoImpl userDao = new UserDaoImpl();
			userDao.addOrUpdate(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 文件上传
	 * @throws Exception
	 */
	public void fileUp11() throws Exception {
		 response = ServletActionContext.getResponse();
	     response.setContentType("text/html;charset=utf-8"); 
	     String fileBasePath = ServletActionContext.getServletContext().getRealPath("");
	           
	        File fullPath =  new File("c:" + "/upload/");
	           if(!fullPath.exists()){ 
	              fullPath.mkdirs();  
	           }    
	       BufferedInputStream bis = null;
	       BufferedOutputStream bos =null;
	       try{  
	            for(int i = 0; i <file.length; i++){ 
	                File file1 = file[i];
	                System.out.println("fileFileName :"+fileFileName);
	             try{ 
	                 bis = new BufferedInputStream(new FileInputStream(file1));
	              //String fileName = new Date().getTime()+".xls";  //重新定义文件的名字和格式
	                 String path=fullPath.getPath()+"/"+fileFileName;
	                 bos = new BufferedOutputStream(new FileOutputStream(new File(path))); 
	                 Streams.copy(bis, bos, true); 
	                 }
	             catch(Exception e){e.printStackTrace(); 
	                 }         
	          }    
	        response.getWriter().print("{success:true,message:'成功'}");  
	        }   
	       catch (Exception e){  
	                                                    
	               response.setContentType("text/html;charset=utf-8");  
	               response.getWriter().print("{success:false,message:'失败'}");  
	               e.printStackTrace();  
	          }  
 }
		
/************************************************************/
	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public String[] getUploadFileNames() {
		return uploadFileNames;
	}

	public void setUploadFileNames(String[] uploadFileNames) {
		this.uploadFileNames = uploadFileNames;
	}

	public String[] getUploadContentTypes() {
		return uploadContentTypes;
	}

	public void setUploadContentTypes(String[] uploadContentTypes) {
		this.uploadContentTypes = uploadContentTypes;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	@Override
	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	
}
